package leetcode.hot100;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 17. 电话号码的字母组合
 * <p>
 * digits = "23"    ["ad","ae","af","bd","be","bf","cd","ce","cf"]
 * digits = ""      []
 * digits = "2"     ["a","b","c"]
 *
 * @author: yjf
 * @date: 2024/11/28
 */
public class LeetCode17 {

    static Map<Character, char[]> map = new HashMap<>();
    static {
        map.put('2', new char[]{'a', 'b', 'c'});
        map.put('3', new char[]{'d', 'e', 'f'});
        map.put('4', new char[]{'g', 'h', 'i'});
        map.put('5', new char[]{'j', 'k', 'l'});
        map.put('6', new char[]{'m', 'n', 'o'});
        map.put('7', new char[]{'p', 'q', 'r', 's'});
        map.put('8', new char[]{'t', 'u', 'v'});
        map.put('9', new char[]{'w', 'x', 'y', 'z'});
    }

    public static List<String> letterCombinations(String digits) {
        if (digits == null || digits.isEmpty()) {
            return new ArrayList<String>();
        }
        List<String> res = new ArrayList<>();
        process(new char[digits.length()], digits, 0, res);
        return res;
    }

    public static void process(char[] chars, String digits, int level, List<String> res) {
        if (level == digits.length()) {
            res.add(charToString(chars));
            return;
        }
        char[] number = map.get(digits.charAt(level));
        for (int i = 0; i < number.length; i++) {
            chars[level] = number[i];
            process(chars, digits, level + 1, res);
        }
    }

    public static String charToString(char[] chars) {
        StringBuilder sb = new StringBuilder();
        for (char c : chars) {
            sb.append(c);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(letterCombinations("23"));
        System.out.println(letterCombinations(""));
        System.out.println(letterCombinations("2"));
    }
}
